(function($) {
	var $this = null;
	$(function() {
		var teamDivHtm = "<div id=\"teamListPanel_yinying\" class=\"yinying\" style='z-index:900;'></div><div id=\"teamListPanel\" class=\"hideDiv\" style='z-index:901'>" + "<div style=\"float:right;margin-bottom:5px;width:20px;height:20px;line-height:20px;" + "text-align:center;background-color:#eee;margin-bottom:5px;border:1px solid #ccc;\" >" + " <span style=\"cursor: pointer;font-size: 14px;\"  id=\"teamListPanel_close\" >X</span>" + " </div><table cellpadding=\"4\" cellspacing=\"0\" border=\"1\" width=\"100%\">" + "<thead><tr><th>分组</th><th>队伍名称</th><th>操作</th></tr></thead> <tbody id='teamListPanel_data'></tbody> </table><div id=\"teamPager\" class='shopPager'></div></div>";
		$("body").prepend(teamDivHtm);
		$("#teamListPanel_close").bind("click", closeTeamListPanel);
		// 关闭
		$("#teamListPanel_search").bind("click", search);
		// 查询
	});
	/**
	 * 显示商铺列表层
	 */
	function openTeamListPanel(id) {
		$this = $("#"+id);
		$this.blur();
		$("#teamListPanel_searchName").val('');
		search();
	}
	
	/**
	 * 加载数据
	 */
	function search() {
		new Pagination({
			method : 'get', // 请求方法 post|get
			type : 'json', // 返回内容格式，json|text //NOT SUPPORT
			url : '../team/find/teamList/', // 请求地址，注意函数会自定添加分页的两个参数 ‘0/10’
			// 使用post 方法是的请求参数
			pageSize : 10, // 页面大小
			startIndex : 0, // 服务器提供的接口页码是从几开始的，建议从0开始
			success : printDataToPage, // state 为 0 时，回调
			pagerHandler : 'teamPager', // 显示分页的 div 的 id
			last : last
		});
	}

	function last(message) {
		/*if(message.result.len==0){
			alert("暂无可用的优惠券");
			return false;
		}*/
		$("#teamListPanel, #teamListPanel_yinying").show();
		addMargin($("#teamListPanel"));
	}

	function printDataToPage(message) {

		var $tbody = $("#teamListPanel_data");
		$tbody.empty();
		
		// 清空数据
		var len = message.result.data.length;
		// 返回条目数
		
		for (var i = 0; i < len; i++) {
			// "id", "name","mp","contact");
			var Name = getHtmlString(getHeadIndexValue(i, message, 'name'));
			var groupType = getHtmlString(getHeadIndexValue(i, message, 'groupType'));
			var ID = getHtmlString(getHeadIndexValue(i, message, 'id'));
			
			var select = "<td><input class=\"common_btn\" type=\"button\" onclick=\"__teamSelected('" + ID + "','" + Name + "')\" value=\"选择\"></input></td>";

			var tr = "<tr><td>" + groupType + "</td>" + "<td>" + Name + "</td>" + select + "</tr>";
			$tbody.append($(tr));
		}
	}

	/**
	 * 关闭
	 */
	function closeTeamListPanel() {
		$("#teamListPanel, #teamListPanel_yinying").hide();
	}

	/**
	 * 选择并关闭
	 */
	function __teamSelected(teamId, teamName) {
		$this.empty();
		$this.append("<option value='" + teamId + "' selected=\"selected\" >" + teamName + "</option>");
		$("#teamListPanel, #teamListPanel_yinying").hide();

		if (window.teamSelected) {
			window.teamSelected(teamId, teamName);
		}
	}
	
	function searchTeam(){
		search(carSysId)
	}

	window.__teamSelected = __teamSelected;
	window.openTeamListPanel = openTeamListPanel;
	window.closeTeamListPanel = closeTeamListPanel;
	window.closeTeamListPanel = closeTeamListPanel;
	window.searchTeam = searchTeam;

})(jQuery);
